#include "mode_cooling.h"
#include "main_motor.h"
#include "actuator_control.h"
#include "timer_utils.h"

// 制冷模式内部状态
static struct {
    bool comp_protection;
    uint32_t comp_run_time;
    bool cooling_stable;
    uint32_t stable_time;
    uint32_t last_mixer_toggle;
    bool mixer_cycle;
} s_cool_state;

// 制冷模式进入
void cooling_mode_enter(ModeConfig* config) {
    memset(&s_cool_state, 0, sizeof(s_cool_state));
    
    // 初始化电机配置
    MotorConfig motor_cfg = {
        .min_current = config->cooling_min_current,
        .max_current = config->cooling_max_current,
        .start_timeout = 5000,
        .retry_count = 3,
        .protection_time = 30000
    };
    motor_init(&motor_cfg);
    
    // 初始化制冷状态
    actuator_set_bypass_valve(false);  // 关闭旁通阀(制冷模式)
    s_cool_state.last_mixer_toggle = get_system_time_ms();
    
    printf("Entering cooling mode\n");
}

// 制冷模式处理
void cooling_mode_process(ModeConfig* config, SensorData* sensors, DeviceState* state) {
    uint32_t current_time = get_system_time_ms();
    
    // 处理电机状态
    motor_process(sensors->motor_current);
    state->motor_running = (motor_get_state() == MOTOR_STATE_RUNNING);
    
    // 检查电机错误
    if (motor_get_error() != MOTOR_ERROR_NONE) {
        state->in_error = true;
        state->error_code = ERROR_MOTOR_CURRENT;
        return;
    }
    
    // 压缩机运行时间统计和保护
    if (state->comp_running && !s_cool_state.comp_protection) {
        s_cool_state.comp_run_time += 1000;  // 假设1秒调用一次
        // 连续运行30分钟进入保护
        if (s_cool_state.comp_run_time >= 30 * 60 * 1000) {
            s_cool_state.comp_protection = true;
            actuator_set_compressor(false);
            state->comp_running = false;
        }
    } else if (!state->comp_running && s_cool_state.comp_protection) {
        // 保护状态下停机3分钟后解除
        if (s_cool_state.comp_run_time >= 33 * 60 * 1000) {
            s_cool_state.comp_protection = false;
            s_cool_state.comp_run_time = 0;
        }
    }
    
    // 温度控制逻辑
    if (sensors->cabinet_temp > config->cooling_target + config->cooling_hyst) {
        if (!state->comp_running && !s_cool_state.comp_protection) {
            actuator_set_compressor(true);
            actuator_set_cooling_valve(true);
            state->comp_running = true;
            state->cooling_valve = true;
        }
    } else if (sensors->cabinet_temp < config->cooling_target - config->cooling_hyst / 2) {
        if (state->comp_running) {
            actuator_set_compressor(false);
            actuator_set_cooling_valve(false);
            state->comp_running = false;
            state->cooling_valve = false;
        }
    }
    
    // 膨化泵与电机同步
    if (state->motor_running && !state->pump_running) {
        actuator_set_pump(true);
        state->pump_running = true;
    } else if (!state->motor_running && state->pump_running) {
        actuator_set_pump(false);
        state->pump_running = false;
    }
    
    // 原料搅拌控制（每2分钟运行30秒）
    if (current_time - s_cool_state.last_mixer_toggle > 60000) {  // 1分钟检查一次
        s_cool_state.last_mixer_toggle = current_time;
        s_cool_state.mixer_cycle = !s_cool_state.mixer_cycle;
        
        if (s_cool_state.mixer_cycle) {
            actuator_set_mixer(true);
            state->mixer_running = true;
        } else {
            actuator_set_mixer(false);
            state->mixer_running = false;
        }
    }
}

// 制冷模式退出
void cooling_mode_exit(void) {
    motor_stop();
    actuator_set_compressor(false);
    actuator_set_cooling_valve(false);
    actuator_set_pump(false);
    actuator_set_mixer(false);
    
    printf("Exiting cooling mode\n");
}
